/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.yanzhenjie.andserver.dialog;

import ohos.app.Context;
import ohos.global.configuration.DeviceCapability;

public class DisplayUtils {
    // 屏幕宽度
    public static int getWindowWidth(Context context) {
        return context.getResourceManager().getDeviceCapability().width * context.getResourceManager().getDeviceCapability().screenDensity / 160;
    }

    //屏幕高度
    public static int getWindowHeight(Context context) {
        return context.getResourceManager().getDeviceCapability().height * context.getResourceManager().getDeviceCapability().screenDensity / 160;
    }

    // 屏幕宽度（像素）
    public static int getWindowPixelWidth(Context context) {
        DeviceCapability metric = context.getResourceManager().getDeviceCapability();
        return metric.width;
    }

    // 屏幕高度（像素）
    public static int getWindowPixelHeight(Context context) {
        DeviceCapability metric = context.getResourceManager().getDeviceCapability();
        return metric.height;
    }


    // 将px值转换为dip或dp值
    public static int px2dip(Context context, float pxValue) {
        final double scale = (double) context.getResourceManager().getDeviceCapability().screenDensity / 160;
        return (int) (pxValue / scale + 0.5f);
    }

    // 将dip或dp值转换为px值
    public static int dip2px(Context context, float dipValue) {
        final double scale = (double) context.getResourceManager().getDeviceCapability().screenDensity / 160;
        return (int) (dipValue * scale + 0.5f);
    }

    // 将px值转换为sp值
    public static int px2sp(Context context, float pxValue) {
        final double fontScale = (double) context.getResourceManager().getDeviceCapability().screenDensity / 160;
        return (int) (pxValue / fontScale + 0.5f);
    }

    // 将sp值转换为px值
    public static int sp2px(Context context, float spValue) {
        final double fontScale =(double) context.getResourceManager().getDeviceCapability().screenDensity / 160;
        return (int) (spValue * fontScale + 0.5f);
    }

}
